package manager.ota

import event.OtaProgressChanged
import ext.log
import ext.rlog
import org.greenrobot.eventbus.EventBus


class OtaServiceManager private constructor() {

    companion object {

        val Instance: OtaServiceManager by lazy { OtaServiceManager() }
    }

    private var mService: OtaService? = null

    fun startUpdate(firmware: ByteArray, version: Int) {

        if (mService != null) {
            rlog("已经正在升级")
            return
        }
        EventBus.getDefault().post(OtaProgressChanged(0, 0, OtaProgressChanged.STATUS_START))
        mService = OtaService(firmware, version, ::quit)
        mService!!.start()
    }

    fun quit(flag: Boolean) {
        mService = null
        log("远程升级退出:$flag")
    }

    fun onMessageReady(byteArray: ByteArray) {
        if (mService == null) {
            return
        }
        mService!!.post( byteArray )
    }
}
